package com.huang.gulimail.product.service.impl;

import com.huang.gulimail.product.entity.AttrEntity;
import com.huang.gulimail.product.service.AttrService;
import com.huang.gulimail.product.vo.AttrGroupWithAttrsVo;
import jodd.bean.BeanUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huang.common.utils.PageUtils;
import com.huang.common.utils.Query;

import com.huang.gulimail.product.dao.AttrGroupDao;
import com.huang.gulimail.product.entity.AttrGroupEntity;
import com.huang.gulimail.product.service.AttrGroupService;
import org.springframework.util.StringUtils;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {

    @Autowired
    AttrService attrService;
    @Override
    public PageUtils queryPage(Map<String, Object> params, Long catelogId) {

            String key=(String)params.get("key");
            QueryWrapper<AttrGroupEntity> wrapper = new QueryWrapper<AttrGroupEntity>();
            if(StringUtils.hasText(key)){

//                wrapper.and(obj->{
//                    obj.eq("attr_group_id",key).or().like("attr_group_name",key);
//                });
                wrapper.eq("attr_group_id",key).or().like("attr_group_name",key);
   
            }
            if(catelogId!=0){
                wrapper.eq("catelog_id",catelogId);
            }
            IPage<AttrGroupEntity> page = this.page(
                    new Query<AttrGroupEntity>().getPage(params),
                    wrapper
            );
            return new PageUtils(page);
        }

    @Override
    public List<AttrGroupWithAttrsVo> getAttrGroupWithAttrsByCatlogId(Long catelogId) {
        //查询分组
        List<AttrGroupEntity>attrGroupEntities=this.list(new QueryWrapper<AttrGroupEntity>().eq("catelog_id",catelogId));
        List<AttrGroupWithAttrsVo> collect = attrGroupEntities.stream().map(group -> {
            AttrGroupWithAttrsVo attrGroupWithAttrsVo = new AttrGroupWithAttrsVo();
            BeanUtils.copyProperties(group,attrGroupWithAttrsVo);
            List<AttrEntity> attrs = attrService.getRelationAttr(attrGroupWithAttrsVo.getAttrGroupId());
            attrGroupWithAttrsVo.setAttrs(attrs);

            return attrGroupWithAttrsVo;
        }).collect(Collectors.toList());
        return collect;
    }


}